cmake_minimum_required(VERSION 3.10)
project(drillpipe-count-pipeline)
set(CMAKE_CXX_STANDARD 17)
# 这是一个自定义函数，用于检查和替换路径。如果传入的路径是绝对路径，则不做任何操作；
# 否则，检查相对路径是否存在，如果存在则转换为绝对路径，并替换原路径；如果不存在则报错。
function(check_and_replace_path ARG_NAME) 
    if(IS_ABSOLUTE ${${ARG_NAME}})
        return()
    endif()
    set(PATH_TO_CHECK ${CMAKE_CURRENT_BINARY_DIR}/${${ARG_NAME}})
    if(EXISTS ${PATH_TO_CHECK})
        message("Path ${PATH_TO_CHECK} exists")
        get_filename_component(ABSOLUTE_PATH ${PATH_TO_CHECK} ABSOLUTE)
        if(EXISTS ${ABSOLUTE_PATH})
            set(${ARG_NAME} ${ABSOLUTE_PATH} PARENT_SCOPE)
        else()
            message(FATAL_ERROR "Invalid path!")
        endif()
    else()
        message(FATAL_ERROR "Path ${PATH_TO_CHECK} does not exist")
    endif()
endfunction()

# set(CMAKE_CXX_FLAGS "-O3")
# 这行代码设置了构建类型为 Debug。
set(CMAKE_BUILD_TYPE "Debug")
# 这行代码设置了可执行文件的输出路径为项目源代码目录
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
message("cur dir: ${PROJECT_SOURCE_DIR}")

if (NOT DEFINED TARGET_ARCH)
    set(TARGET_ARCH soc)
endif()

if (NOT DEFINED SDK)
    set(SDK /workspace/soc-sdk-24.04.01/)
endif()


# PCIe 模式：配置 FFmpeg、OpenCV 和 libsophon 的路径和库，并添加可执行文件 yolov8_bmcv.pcie。
if (${TARGET_ARCH} STREQUAL "pcie")
    message( "${TARGET_ARCH} mode, starting......")
    # set(lib_DIR /usr/lib/x84_64-linux-gnu)
    # link_directories(${lib_DIR})
    # use ffmpeg
    set(FFMPEG_DIR  /opt/sophon/sophon-ffmpeg-latest/lib/cmake)
    find_package(FFMPEG REQUIRED)
    include_directories(${FFMPEG_INCLUDE_DIRS})
    link_directories(${FFMPEG_LIB_DIRS})
    # use opencv
    set(OpenCV_DIR  /opt/sophon/sophon-opencv-latest/lib/cmake/opencv4)
    find_package(OpenCV REQUIRED)
    include_directories(${OpenCV_INCLUDE_DIRS})
    link_directories(${OpenCV_LIB_DIRS})
    # use libbmrt libbmlib
    find_package(libsophon REQUIRED)
    include_directories(${LIBSOPHON_INCLUDE_DIRS})
    link_directories(${LIBSOPHON_LIB_DIRS})

    # 使用 aux_source_directory 函数收集当前目录下的所有源文件，并使用 add_executable 添加可执行文件
    aux_source_directory(. SRC_FILES)
    add_executable(yolov8_bmcv.pcie ${SRC_FILES})
    # 为可执行文件设置链接库，包括 FFmpeg、OpenCV 和自定义库等
    target_link_libraries(yolov8_bmcv.pcie ${FFMPEG_LIBS} ${OpenCV_LIBS} ${the_libbmlib.so} ${the_libbmrt.so} ${the_libbmcv.so} -lpthread)
# SoC 模式：调用 check_and_replace_path 函数检查 SDK 路径，并设置 C/C++ 编译器和链接库，添加可执行文件 yolov8_bmcv.soc
elseif (${TARGET_ARCH} STREQUAL "soc")
    message( "${TARGET_ARCH} mode, starting......")
    check_and_replace_path(SDK)
    set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
    set(CMAKE_ASM_COMPILER aarch64-linux-gnu-gcc)
    set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
    set(BM_LIBS bmlib bmrt bmcv yuv)
    set(OPENCV_LIBS opencv_imgproc opencv_core opencv_highgui opencv_imgcodecs opencv_videoio)
    include_directories("${SDK}/include/")
    include_directories("${SDK}/include/opencv4")
    include_directories("include/")
    include_directories("include/common")
    include_directories("include/ff_codec")
    link_directories("${SDK}/lib/")
    

    message("SDK: " ${SDK})

    # 使用 aux_source_directory 函数收集当前目录下的所有源文件，并使用 add_executable 添加可执行文件
    aux_source_directory(. SRC_FILES)
    aux_source_directory(src/ SRC_FILES)
    aux_source_directory(src/common SRC_FILES)
    aux_source_directory(src/ff_codec SRC_FILES)
    add_executable(yolov8_bmcv.soc ${SRC_FILES})
    # 为可执行文件设置链接库，包括 FFmpeg、OpenCV 和自定义库等
    target_link_libraries(yolov8_bmcv.soc ${BM_LIBS} ${OPENCV_LIBS} -lpthread -lavcodec -lavformat -lavutil paho-mqtt3c) 

else ()
    message( FATAL_ERROR  "not support mode, eg: soc/pcie, cur: ${TARGET_ARCH}")
endif ()